package net.lzzy.practices.web.admin;

import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messgaes;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.JwtUtil;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_AUTH_ROOT)
public class UserApiController {
    private final UserService service;

    @Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }

    @GetMapping(ApiConstant.ROUTE_USER_ALL)
    public ApiResult getUsers(@RequestParam String token) {
        String uid = JwtUtil.verifyToken(token);
        if (uid == null) {
            return new ApiResult(false, "认证信息已失效，请重新登录！",
                    null);
        }

        User user = service.getById(UUID.fromString(uid));
        if (user == null) {
            return new ApiResult(false, "认证信息无效，请登录", null);
        }
        int[] allowRoles = new int[]{User.TYPE_ADMIN};
        if (Arrays.stream(allowRoles).noneMatch(role -> role == user.getType())) {
            return new ApiResult(false, "没有权限，请登录为管理员！", null);
        }
        List<VmUser> users = new ArrayList<>();
        service.get().forEach(u -> users.add(VmUser.create(u, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_RAGE)
    public ApiResult getPagedUsers(@RequestParam int page, @RequestParam int size, @RequestParam String token) {
        List<VmUser> users = new ArrayList<>();
        service.get(page, size).forEach(u -> users.add(VmUser.create(u, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_SEARCH)
    public ApiResult search(@RequestParam String token, @PathVariable String kw) {
        List<VmUser> users = new ArrayList<>();
        service.search(kw).forEach(u -> users.add(VmUser.create(u, true)));
        return new ApiResult(true, users.size() + "", users);
    }

    @GetMapping(ApiConstant.ROUTE_USER_APPLYING)
    public ApiResult getApplyingTeacher(@RequestParam String token) {
        List<VmUser> users = new ArrayList<>();
        service.getByApplyTeacher(true).forEach(u -> users.add(VmUser.create(u,true)));
        return new ApiResult(true, users.size() + "", users);
    }
    @PostMapping(ApiConstant.ROUTE_USER_APPLYING)
    public ApiResult approve(@RequestParam String token,@RequestParam UUID id){
        User user = service.approveTeacher(id);
        if (user == null) {
            return new ApiResult(false, Messgaes.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Messgaes.SUCCESS.toString(),
                VmUser.create(user,true));
    }

    @PostMapping(ApiConstant.ROUTE_USER_DECLINE)
    public ApiResult decline(@RequestParam String token,@RequestParam UUID id){
        User user = service.approveTeacher(id);
        if (user == null) {
            return new ApiResult(false, Messgaes.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Messgaes.SUCCESS.toString(),
                VmUser.create(user,true));
    }

    @PostMapping(ApiConstant.ROUTE_USER_BAN)
    public ApiResult ban(@RequestParam String token,@RequestParam UUID id){
        User user = service.approveTeacher(id);
        if (user == null) {
            return new ApiResult(false, Messgaes.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Messgaes.SUCCESS.toString(),
                VmUser.create(user,true));
    }

}



